﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class register : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString());
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        conn.Open();
        string str = "select * from userinfo where name='"+name.Text.Trim()+"'";
        SqlCommand comm1 = new SqlCommand(str, conn);
        SqlDataReader dr = comm1.ExecuteReader();
        if (dr.HasRows)
        {
            Response.Write("<script>alert('一个账号只能注册一次！')</script>");
        }
        else if (password1.Text.ToString() != password2.Text.ToString())
        {
            Response.Write("<script>alert('2次输入密码不一样，请您核对后输入！')</script>");
        }
        else
        {
            dr.Close();
            SqlTransaction st = conn.BeginTransaction();
            SqlCommand comm = conn.CreateCommand();
            comm.Transaction = st;
            try
            {
                comm.CommandText = "insert into userinfo(name,password) values('" + name.Text.ToString() + "','" + password1.Text.ToString() + "')";
                comm.ExecuteNonQuery();
                Response.Write("<script>alert('恭喜您成功注册！')</script>");
                st.Commit();
            }
            catch (Exception ex)
            {
                st.Rollback();
                Response.Write("<script>alert('对不起注册失败！,请重试..')</script>");
            }
            finally
            {
                conn.Close();
                comm.Dispose();
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("login.aspx");
    }
}